home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / delitem.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  13KB  |  342 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "delitem.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "ListView_DeleteItem()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LPCTSTR lpszData[4]  = { "Circle", "Rectangle", "Cross", "Check" };
  108. LPCTSTR lpszColor[4] = { "Yellow", "Red",       "Black", "Black" };
  109.  
  110. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  111. {
  112. static HWND  hList    = NULL;
  113. static int   nCurView = IDM_VIEWLARGE;
  114.  
  115.    switch( uMsg )
  116.    {
  117.       case WM_CREATE  :
  118.               {
  119.                  HIMAGELIST hImage, hSmall;
  120.  
  121.                  InitCommonControls();
  122.  
  123.                  // Create the image lists and the list view.
  124.                  //..........................................
  125.                  hImage = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  126.                  hSmall = ImageList_Create( 16, 16, ILC_COLOR4 | ILC_MASK, 4, 1 );
  127.                  hList  = CreateWindowEx( 0, WC_LISTVIEW, "",
  128.                                           WS_CHILD | WS_VISIBLE | LVS_ICON,
  129.                                           10, 100, 100, 100,
  130.                                           hWnd,
  131.                                           (HMENU)1,
  132.                                           hInst,
  133.                                           NULL);
  134.  
  135.                  if ( hList && hImage && hSmall )
  136.                  {
  137.                     LV_COLUMN col;
  138.                     int       i;
  139.  
  140.                     // Set the large and small image lists.
  141.                     //.....................................
  142.                     ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
  143.                     ListView_SetImageList( hList, hSmall, LVSIL_SMALL );
  144.  
  145.                     // Add the images to the image list.
  146.                     //..................................
  147.                     for( i=0; i<4; i++ )
  148.                     {
  149.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  150.                        ImageList_AddIcon( hSmall, LoadIcon( hInst, lpszData[i] ) );
  151.                     }
  152.  
  153.                     // Add the columns for the report view.
  154.                     //.....................................
  155.                     col.mask    = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
  156.                     col.fmt     = LVCFMT_LEFT; 
  157.                     col.cx      = 100; 
  158.  
  159.                     col.pszText  = "Name"; 
  160.                     col.iSubItem = 0;
  161.                     ListView_InsertColumn( hList, 0, &col );
  162.                     
  163.                     col.pszText  = "Color"; 
  164.                     col.iSubItem = 1;
  165.                     ListView_InsertColumn( hList, 1, &col );
  166.                  }
  167.               }
  168.               break;
  169.  
  170.       case WM_SIZE :
  171.               if ( hList )
  172.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  173.  
  174.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  175.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  176.               break;
  177.  
  178.       case WM_INITMENU :
  179.               CheckMenuRadioItem( (HMENU)wParam, 
  180.                                   IDM_VIEWLARGE, IDM_VIEWDETAILS, nCurView, MF_BYCOMMAND );
  181.  
  182.               // Enable the delete and duplicate menu commands
  183.               // if there are any items selected in the list view.
  184.               //..................................................
  185.               EnableMenuItem( (HMENU)wParam, IDM_DUPLICATE, MF_BYCOMMAND | 
  186.                               ListView_GetSelectedCount( hList ) ? MF_ENABLED : MF_GRAYED );
  187.  
  188.               EnableMenuItem( (HMENU)wParam, IDM_DELETE, MF_BYCOMMAND | 
  189.                               ListView_GetSelectedCount( hList ) ? MF_ENABLED : MF_GRAYED );
  190.  
  191.               // Enable the clear all menu command if there are any itmes in the list.
  192.               //......................................................................
  193.               EnableMenuItem( (HMENU)wParam, IDM_DELETEALL, MF_BYCOMMAND | 
  194.                               ListView_GetItemCount( hList ) ? MF_ENABLED : MF_GRAYED );
  195.               break; 
  196.  
  197.       case WM_COMMAND :
  198.               switch( LOWORD( wParam ) )
  199.               {
  200.                  case IDM_INSCIRCLE :
  201.                  case IDM_INSRECT :
  202.                  case IDM_INSCROSS :
  203.                  case IDM_INSCHECK :
  204.                         {
  205.                            LV_ITEM item;
  206.  
  207.                            // Insert a new item into the list view.
  208.                            //......................................
  209.                            item.mask     = LVIF_TEXT | LVIF_IMAGE;
  210.                            item.pszText  = (LPTSTR)lpszData[ LOWORD( wParam )-IDM_INSCIRCLE ];
  211.                            item.iItem    = ListView_GetItemCount( hList );
  212.                            item.iSubItem = 0;
  213.                            item.iImage   = LOWORD( wParam )-IDM_INSCIRCLE;
  214.                            ListView_InsertItem( hList, &item );
  215.  
  216.                            item.mask     = LVIF_TEXT;
  217.                            item.pszText  = (LPTSTR)lpszColor[ LOWORD( wParam )-IDM_INSCIRCLE ];
  218.                            item.iSubItem = 1;
  219.                            ListView_SetItem( hList, &item );
  220.                         }
  221.                         break;
  222.  
  223.                  case IDM_DUPLICATE :
  224.                         {
  225.                            LV_ITEM item;
  226.                            char    szBuf[50];
  227.                            int     nItem = -1;
  228.  
  229.                            item.mask     = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  230.                            item.iSubItem = 0;
  231.  
  232.                            // Duplicate all the selected items.
  233.                            //...............................
  234.                            while( (nItem = ListView_GetNextItem( hList, nItem, 
  235.                                                         LVNI_ALL | LVNI_SELECTED)) > -1 )
  236.                            {
  237.                               item.iItem     = nItem;
  238.                               item.pszText   = szBuf;
  239.                               item.cchTextMax = sizeof( szBuf )-1;
  240.                               ListView_GetItem( hList, &item );
  241.  
  242.                               item.iItem = ListView_GetItemCount( hList );
  243.                               ListView_InsertItem( hList, &item );
  244.  
  245.                               ListView_GetItemText( hList, nItem, 1, szBuf, sizeof( szBuf )-1 );
  246.                               ListView_SetItemText( hList, item.iItem, 1, szBuf ); 
  247.                            }
  248.  
  249.                         }
  250.                         break;
  251.  
  252.                  case IDM_DELETE :
  253.                         {
  254.                            int nItem = -1;
  255.  
  256.                            // Delete all the selected items.
  257.                            //...............................
  258.                            while( (nItem = ListView_GetNextItem( hList, nItem, 
  259.                                                         LVNI_ALL | LVNI_SELECTED)) > -1 )
  260.                            {
  261.                               ListView_DeleteItem( hList, nItem );
  262.                            }
  263.  
  264.                            // Make sure the icons are arranged to fill
  265.                            //  in the gaps left from the delete icons.
  266.                            //.........................................
  267.                            ListView_Arrange( hList, LVA_DEFAULT );
  268.                         }
  269.                         break;
  270.  
  271.                  case IDM_DELETEALL :
  272.                         // Delete all the items in the list view.
  273.                         //.......................................
  274.                         ListView_DeleteAllItems( hList );
  275.                         break;
  276.  
  277.                  case IDM_VIEWLARGE   :
  278.                  case IDM_VIEWSMALL   :
  279.                  case IDM_VIEWLIST    :
  280.                  case IDM_VIEWDETAILS :
  281.                         // Change the view of the list view.
  282.                         //..................................
  283.                         SetWindowLong( hList, GWL_STYLE, WS_CHILD | WS_VISIBLE | 
  284.                                        (LOWORD( wParam ) == IDM_VIEWLARGE ? LVS_ICON :
  285.                                         LOWORD( wParam ) == IDM_VIEWSMALL ? LVS_SMALLICON :
  286.                                         LOWORD( wParam ) == IDM_VIEWLIST  ? LVS_LIST :
  287.                                         LVS_REPORT ) );
  288.  
  289.                         nCurView = LOWORD( wParam );
  290.                         break;
  291.  
  292.                  case IDM_ABOUT :
  293.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  294.                         break;
  295.  
  296.                  case IDM_EXIT :
  297.                         DestroyWindow( hWnd );
  298.                         break;
  299.               }
  300.               break;
  301.       
  302.       case WM_DESTROY :
  303.               if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
  304.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
  305.  
  306.               if ( ListView_GetImageList( hList, LVSIL_SMALL ) )
  307.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_SMALL ) );
  308.  
  309.               PostQuitMessage(0);
  310.               break;
  311.  
  312.       default :
  313.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  314.    }
  315.  
  316.    return( 0L );
  317. }
  318.  
  319.  
  320. LRESULT CALLBACK About( HWND hDlg,           
  321.                         UINT message,        
  322.                         WPARAM wParam,       
  323.                         LPARAM lParam)
  324. {
  325.    switch (message) 
  326.    {
  327.        case WM_INITDIALOG: 
  328.                return (TRUE);
  329.  
  330.        case WM_COMMAND:                              
  331.                if (   LOWORD(wParam) == IDOK         
  332.                    || LOWORD(wParam) == IDCANCEL)    
  333.                {
  334.                        EndDialog(hDlg, TRUE);        
  335.                        return (TRUE);
  336.                }
  337.                break;
  338.    }
  339.  
  340.    return (FALSE); 
  341. }
  342.